package net.ghsoft.Ctrl
{
	import flash.events.MouseEvent;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.controls.Label;
	import mx.events.FlexEvent;
	
	import spark.components.Group;
	import spark.components.HGroup;
	import spark.events.IndexChangeEvent;
	
	[Event(name="change", type="spark.events.IndexChangeEvent")]
	
	public class MySelectBox extends HGroup
	{
		
		public function MySelectBox()
		{
			addEventListener(FlexEvent.CREATION_COMPLETE, group_creationCompleteHandler);
			super();
		}
		
		private var _lText:Label;
		protected function group_creationCompleteHandler(event:FlexEvent):void
		{
			verticalAlign = "middle";
			
			var lLeft:Label = new Label();
			lLeft.text = "◄";
			lLeft.buttonMode = true;
			lLeft.addEventListener(MouseEvent.CLICK, lLeft_clickHandler);
			addElement(lLeft);
			
			var gText:Group = new Group();
			gText.percentWidth = 100;
			addElement(gText);
			
			_lText = new Label();
//			_lText.text = "aaa";
			_lText.horizontalCenter = 0;
			gText.addElement(_lText);
			callLater(setData);
			
			var lRight:Label = new Label();
			lRight.text = "►";
			lRight.buttonMode = true;
			lRight.addEventListener(MouseEvent.CLICK, lRight_clickHandler);
			addElement(lRight);
		}
		
		private function setData():void
		{
			if(selectDataValue != "")
			{
				selectDataValue = selectDataValue;
			}
		}
		
		protected function lLeft_clickHandler(event:MouseEvent):void
		{
			// TODO Auto-generated method stub
			if(_selectedIndex > 0)
			{
				var oi:int = _selectedIndex;
				_selectedIndex --;
				_lText.text = _arrSelectDataValue[selectedIndex];
				dispatchEvent(new IndexChangeEvent("change", false ,false, oi, _selectedIndex));
			}
		}
		
		protected function lRight_clickHandler(event:MouseEvent):void
		{
			// TODO Auto-generated method stub
			if(_selectedIndex < _arrSelectDataValue.length - 1)
			{
				var oi:int = _selectedIndex;
				_selectedIndex ++;
				_lText.text = _arrSelectDataValue[selectedIndex];
				dispatchEvent(new IndexChangeEvent("change", false ,false, oi, _selectedIndex));
			}
		}
		
		private var _selectDataValue:String = "";
		
		private var _arrSelectDataValue:ArrayCollection = new ArrayCollection();
		
		public function set selectDataValue(value:String):void
		{
			_selectDataValue = value;
			if(_lText != null)
			{
				var arrTemp:Array = _selectDataValue.split("|");
				if(arrTemp.length > 0)
				{
					_arrSelectDataValue = new ArrayCollection(arrTemp);
					selectedIndex = 0;
					_lText.text = _arrSelectDataValue[selectedIndex];
				}
			}
		}
		
		public function get selectDataValue():String
		{
			return _selectDataValue;
		}
		
		private var _selectedIndex:int = -1;

		public function set selectedIndex(value:int):void
		{
			if(value >= 0 && value < _arrSelectDataValue.length)
			{
				_selectedIndex = value;
				_lText.text = _arrSelectDataValue[selectedIndex];
			}
		}
		
		public function get selectedIndex():int
		{
			return _selectedIndex;
		}

		public function set selectedValue(value:String):void
		{
			var index:int = _arrSelectDataValue.getItemIndex(value)
			
			if(index >= 0)				
			{
				selectedIndex = index;
			}
		}
		
		public function get selectedValue():String
		{
			return _lText.text;
		}
		

	}
}